﻿namespace Shanda.DevFx.Security.Web.Pages
{
    using Shanda.DevFx.Core;
    using Shanda.DevFx.Security;
    using Shanda.DevFx.Security.Web;
    using System;
    using System.Web.UI.WebControls;

    public class PermissionPage : Shanda.DevFx.Security.Web.BasePage
    {
        protected Label lblMessage;
        protected Repeater repeater;

        private void BindData()
        {
            this.repeater.set_DataSource(PermissionManager.SortPermissions(Securitor.PermissionManager.ListItem(), "permissionNo", false));
            this.repeater.DataBind();
        }

        private void InitializeComponent()
        {
            this.repeater.add_ItemCreated(new RepeaterItemEventHandler(this, (IntPtr) this.repeater_ItemCreated));
            this.repeater.add_ItemCommand(new RepeaterCommandEventHandler(this, (IntPtr) this.repeater_ItemCommand));
            base.add_Load(new EventHandler(this.Page_Load));
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.get_IsPostBack())
            {
                this.BindData();
            }
        }

        private void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            IAOPResult result = null;
            if (e.get_CommandName() == "DELETE")
            {
                result = Securitor.PermissionManager.DeleteItem((string) e.get_CommandArgument());
            }
            if (result != null)
            {
                if (result.IsSuccess)
                {
                    this.lblMessage.set_Text("操作成功");
                    this.BindData();
                }
                else
                {
                    this.lblMessage.set_Text(result.ResultDescription);
                }
            }
        }

        private void repeater_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            LinkButton button = e.get_Item().FindControl("btnDelete") as LinkButton;
            if (button != null)
            {
                button.get_Attributes().set_Item("onclick", "return confirm('确认删除？')");
            }
        }
    }
}

